package com.writable;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class FlowMapper extends Mapper<IntWritable, Text, Text, FlowBean> {

    // 输出的key，代表手机号
    private Text k = new Text();

    // 输出的value，表示实际的数据
    private FlowBean v = new FlowBean();

    @Override
    protected void map(IntWritable key, Text value, Context context) throws IOException, InterruptedException {
        String[] line = value.toString().split("\t");

        // 根据分割的行内容，获取手机号，上行流量，下行流量
        String phone = line[0];
        String up = line[line.length - 3];
        String down = line[line.length - 2];

        //  设置key，手机号
        k.set(phone);

        // 设置上下行流量，最后的总流量则是上下相加
        v.setUpFlow(Long.parseLong(up));
        v.setDownFlow(Long.parseLong(down));
        v.setSumFlow();

        // 写出到Reduce
        context.write(k, v);

    }
}
